﻿using Microsoft.BizTalk.BaseFunctoids;
using System;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Text;

namespace BizTalk.ESB.Extensions.SSO.BizTalk
{
    public class SsoFunctoid : BaseFunctoid
    {
        public SsoFunctoid() : base()
        {
            this.ID = 7500;
            SetupResourceAssembly("BizTalk.ESB.Extensions.SSO.BizTalk.SsoFunctoidResources", Assembly.GetExecutingAssembly());
            SetName("IDS_SSOGETCONFIGVALUEFUNCTOID_NAME");
            SetTooltip("IDS_SSOGETCONFIGVALUEFUNCTOID_TOOLTIP");
            SetDescription("IDS_SSOGETCONFIGVALUEFUNCTOID_DESCRIPTION");
            SetBitmap("SsoConfig");

            this.SetMinParams(2);
            this.SetMaxParams(2);

            SetExternalFunctionName(GetType().Assembly.FullName, "BizTalk.ESB.Extensions.SSO.BizTalk.SsoFunctoid", "GetSsoConfigValue");

            this.Category = FunctoidCategory.String;
            this.OutputConnectionType = ConnectionType.AllExceptRecord;

            AddInputConnectionType(ConnectionType.AllExceptRecord); //first input
            AddInputConnectionType(ConnectionType.AllExceptRecord); //second input
        }

        public String GetSsoConfigValue(string SSOApplicationName, string SSOConfigKey)
        {
            try
            {
                var SSOConfigValue = System.Convert.ToString(
                    SsoConfigWrapper.TryGetConfigValue(SSOApplicationName, SSOConfigKey, String.Empty));
                return SSOConfigValue;
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Exception from SsoFunctoid: " + e.Message);
                return null;
            }
        }
    }
}
